﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientLibrary;

namespace DocumentDigitalization
{
    public partial class Settings_Form : Form
    {
        SystemState Systemstate;

        public Settings_Form(SystemState sysstate)
        {
            this.Systemstate = sysstate;
            InitializeComponent();
            this.InitFromSystemState();
        }

        public void InitFromSystemState()
        {
            // Init the object - ghostscript
            this.tb_ghostscript_path.Text = this.Systemstate.Ghostscript_path;

            // Init the objects - mysql                 
            this.tb_username_mysql.Text = this.Systemstate.Mysql_user;
            this.tb_password_mysql.Text = this.Systemstate.Mysql_password;
            this.tb_database_mysql.Text = this.Systemstate.Mysql_database;
            this.tb_host_mysql.Text = this.Systemstate.Mysql_host;            

            // Init the objects - xml        
            this.tb_xmlexportpath.Text = this.Systemstate.Xml_path;
               
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.groupBox_mysql_settings.Visible == false)
            {
                this.groupBox_mysql_settings.Visible = true;
            }
            else
            {
                this.groupBox_mysql_settings.Visible = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.groupBox_xml_settings.Visible == false)
            {
                this.groupBox_xml_settings.Visible = true;
            }
            else
            {
                this.groupBox_xml_settings.Visible = false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string ghostscript_path = this.tb_ghostscript_path.Text;
            this.Systemstate.Ghostscript_path = ghostscript_path;
            this.Systemstate.SaveStateToXML();
        }

        private void btn_find_ghostscript_path_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.folderBrowserDialog.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                string foldername = this.folderBrowserDialog.SelectedPath;
                this.tb_ghostscript_path.Text = foldername;
            }
        }

        private void tabPage_ghostscript_Enter(object sender, EventArgs e)
        {
            this.InitFromSystemState();
        }

        private void tabPage_export_Enter(object sender, EventArgs e)
        {
            this.InitFromSystemState();
        }

        // SAVE THE EXPORT SETTINGS TO XML
        private void btn_save_export_Click(object sender, EventArgs e)
        {
            string mysql_username = this.tb_username_mysql.Text;
            string mysql_password = this.tb_password_mysql.Text;
            string mysql_database = this.tb_database_mysql.Text;
            string mysql_host = this.tb_host_mysql.Text;
            string xml_path = this.tb_xmlexportpath.Text;

            this.Systemstate.Mysql_user = mysql_username;
            this.Systemstate.Mysql_password = mysql_password;
            this.Systemstate.Mysql_database = mysql_database;
            this.Systemstate.Mysql_host = mysql_host;
            this.Systemstate.Xml_path = xml_path;

            this.Systemstate.SaveStateToXML();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
